package cms.action;

import java.util.ArrayList;
import java.util.List;

import page.PageModel;
import page.Pager;
import cms.business.CMSBusiness;
import cms.hibernate.po.Article;

import com.opensymphony.xwork2.ActionSupport;

public class ArticleAction extends ActionSupport {
	
	private Article article ;
	private CMSBusiness cMSBusiness = new CMSBusiness();
	private List categories ;
	private PageModel pageModel = new PageModel();
	private Pager pager = new Pager();
	public String list() {
		System.out.println("offset:"+this.pager.getOffset());		
		List list = this.cMSBusiness.getArticleList(this.pager.getOffset()/3+1, 3);
		this.pageModel.setDatas(list);
		this.pageModel.setTotal(this.cMSBusiness.getArticleCount());		
		return "list";
	}
	
	public String delete() {
		if(this.cMSBusiness.deleteArticleById(this.article.getId())) {
			addActionMessage("DELETE_SUCCESS");
		} else {
			addActionError("DELETE_FAILED");
		}
		list();
		return "afterdelete";
		
	}
	/*
	 * 进入新建或者编辑文章信息页面
	 */
	@SuppressWarnings("unchecked")
	public String edit() {
		if(article==null||article.getId()==null||article.getId().equals(0))
		{//新建文章
			article=new Article();
		}
		else
			article = this.cMSBusiness.getArticleById(this.article.getId());
		categories = new ArrayList();
		this.categories.addAll(this.cMSBusiness.getCategoryChirldList());
		
		return "edit";
	}
	@SuppressWarnings("unchecked")
	public String saveedit()
	{
		//无论操作是否成功，文章分类列表都是需要在前台显示的
		categories = new ArrayList();
		this.categories.addAll(this.cMSBusiness.getCategoryChirldList());
		
		if(article==null)
		{
			addActionError(getText("SAVE_FAILED"));
			return "save";
		}
		if(article.getTitle().equals(""))
		{//文章标题不能为空
			addActionError(getText("ARTICLE_NAMEBLANK"));			
			return "save";
		}
		if(cMSBusiness.saveArticle(article)==true)
		{
			addActionMessage(getText("SAVE_SUCCESS"));
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));
		}
		
		return "save";
	}
	public void setcMSBusiness(CMSBusiness cMSBusiness) {
		this.cMSBusiness = cMSBusiness;
	}

	public CMSBusiness getcMSBusiness() {
		return cMSBusiness;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article getArticle() {
		return article;
	}

	public void setCategories(List categories) {
		this.categories = categories;
	}

	public List getCategories() {
		return categories;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public Pager getPager() {
		return pager;
	}
}
